<template>
    <div>
        <el-table :data="adminList" border style="width: 100%">
            <el-table-column prop="uid" label="用户编号" width="180" />
            <el-table-column prop="username" label="用户名称" width="180" />
            <el-table-column prop="rolename" label="所属角色" />
            <el-table-column prop="status" label="状态">
                <template v-slot="item">
                    <div>
                        <el-tag v-if="item.row.status == 1" type="success">启用</el-tag>
                        <el-tag v-else type="danger">禁用</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="操作">
                <template v-slot="item">
                    <div>
                        <el-button type="primary" size="small" @click="edit(item.row.uid)">编辑</el-button>
                        <el-button type="danger" size="small" @click="del(item.row.uid)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background layout="prev, pager, next" :page-size="size" :total="total" @current-change="changePage" />
    </div>
</template>

<script lang="ts" setup>
import { defineEmits, computed } from 'vue'
import { useStore } from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'
// 引入接口
import { userDel } from '@/request/app'
let emit = defineEmits(['edit'])
let edit = (uid: number) => {
    emit('edit', uid)
}
// 仓库
const store = useStore()
// 列表
const adminList = computed(() => store.getters['admin/getAdminList'])
// 总数
const total = computed(() => store.getters['admin/getTotal'])
// 条数
const size = computed(()=>store.getters['admin/getSize'])
const getAdmin = () => {
    // 列表
    store.dispatch('admin/adminListAction')
    // 总数
    store.dispatch('admin/adminTotalAction')
}
getAdmin()
// 改变页码数
let changePage = (e:number)=>{
    console.log(e,'页码数');
    store.dispatch('admin/changePageAction',e)
}
// 删除事件
const del = (uid: number) => {
    ElMessageBox.confirm(
        '是否确认删除?',
        '提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            // 调用删除接口
            userDel({ uid })
                .then(res => {
                    console.log(res, '删除成功');
                    if (res.code == 200) {
                        // 成功提示
                        ElMessage.success(res.msg)
                        // 重新调用列表接口渲染
                        getAdmin()
                    } else {
                        // 失败提示
                        ElMessage.success(res.msg)
                    }

                })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: 'Delete canceled',
            })
        })
}

</script>

<style scoped>
</style>
